package com.bw.module_home.viewModel

import androidx.lifecycle.viewModelScope
import com.bw.lib_common.base.BaseViewModel
import com.bw.module_home.intent.HomeSimpleIntent
import com.bw.module_home.model.repository.HomeRepository
import com.bw.module_home.state.HomeSimpleTypeState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class HomeViewModel @Inject constructor(private val repository: HomeRepository):BaseViewModel<HomeSimpleIntent>(){
    private var simpleTypeStateFlow  = MutableStateFlow<HomeSimpleTypeState>(HomeSimpleTypeState.Init)
    val simpleTypeStateFlow_ = simpleTypeStateFlow.asStateFlow()
    private var simpleTypeStateFlow1  = MutableStateFlow<HomeSimpleTypeState>(HomeSimpleTypeState.Init)
    val simpleTypeStateFlow1_ = simpleTypeStateFlow1.asStateFlow()
    private var simpleTypeStateFlow2  = MutableStateFlow<HomeSimpleTypeState>(HomeSimpleTypeState.Init)
    val simpleTypeStateFlow2_ = simpleTypeStateFlow2.asStateFlow()


    /**
     * 1.根据不同的意图处理不同数据请求
     */
    override fun handleIntent() {
        viewModelScope.launch {
            intents.consumeAsFlow().collect{
                when(it){
                    is HomeSimpleIntent.getSimpleType -> {//请求视频类型
                        getSimpleType()
                    }
                    is HomeSimpleIntent.getSimpleVideoByChannelId -> {//根据id获得视频信息
                        getSimpleVideoByChannelId(it.channelId,it.page,it.pageSize)
                    }
                    is HomeSimpleIntent.getRecommendSimpleVideo -> {//请求推荐视频
//                        getRecommendSimpleVideo(it.page, it.pageSize)

                    }

                }
            }
        }
    }

//     fun getRecommendSimpleVideo(page:Int,pageSize:Int) {
//         viewModelScope.launch {
//             // 发送加载状态
//             simpleTypeStateFlow1.value = HomeSimpleTypeState.Loading
//
//             repository.getRecommendSimpleVideo(page, pageSize)
//                 .catch { e ->
//                     simpleTypeStateFlow1.value = HomeSimpleTypeState.Error(e.message.toString())
//                 }.filter {
//                     it != null
//                 }.collect{
//                     if (it.code == 200){
//                         if (it.data.isNotEmpty()){
//                             simpleTypeStateFlow1.value = HomeSimpleTypeState.Success1(it.data)
//                         }else{
//                             simpleTypeStateFlow1.value = HomeSimpleTypeState.Empty
//                         }
//                     }else{
//                         simpleTypeStateFlow1.value = HomeSimpleTypeState.Error(it.msg)
//                     }
//                 }
//         }
//
//    }

    fun getSimpleVideoByChannelId(channelId: String,page: Int,pageSize: Int){
        viewModelScope.launch {
            simpleTypeStateFlow2.value = HomeSimpleTypeState.Loading
            repository.getSimpleVideoByChannelId(channelId,page,pageSize)
                .catch { e ->
                    simpleTypeStateFlow2.value = HomeSimpleTypeState.Error(e.message.toString())
                }.filter {
                    it!=null
                }
                .collect{
                    if(it.code == 200){
                        if(it.data.isNotEmpty()){
                            simpleTypeStateFlow2.value = HomeSimpleTypeState.Success2(it.data)
                        }else{
                            simpleTypeStateFlow2.value = HomeSimpleTypeState.Empty
                        }
                    }
                }
        }
    }

    /**
     * 2.请求的结果更新state
     */
    fun getSimpleType(){
        viewModelScope.launch {
            repository.getSimpleType()
                .catch { e ->
                    simpleTypeStateFlow.value = HomeSimpleTypeState.Error(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if (it.code == 200){
                        if (it.data.isNotEmpty()){
                            simpleTypeStateFlow.value = HomeSimpleTypeState.Success(it.data)
                        }else{
                            simpleTypeStateFlow.value = HomeSimpleTypeState.Empty
                        }
                    }else{
                        simpleTypeStateFlow.value = HomeSimpleTypeState.Error(it.msg)
                    }
                }
        }
    }

}